package uebung2;

public class A1 {
	public static void main(String[] args) {
		MyList<String> l = new MyList<String>();
		l.push_front("Hund");
		l.push_back("Maus");
		l.push_back("Vogel");
		l.delete(1);
		System.out.println(l.size());
		l.print();
	}
}

interface SimpleCollection<T> {
	public int size();

	public T get(int position);

	public void set(int position, T data);

	public void push_front(T data);

	public void push_back(T data);

	public void delete(int position);

	public void print();
}

class MyList<T> implements SimpleCollection<T> {
	Node m_head, m_tail;

	MyList() {
		m_head = m_tail = null;
	}

	class Node {
		Node m_prev;
		Node m_next;
		T m_data;

		Node(T data, Node prev, Node next) {
			m_prev = prev;
			m_next = next;
			m_data = data;
			if (m_prev != null)
				m_prev.m_next = this;
			if (m_next != null)
				m_next.m_prev = this;
		}
	}

	@Override
	public int size() {
		Node temp = m_head;
		int size = 0;

		while (temp != null) {
			temp = temp.m_next;
			++size;
		}
		return size;
	}

	@Override
	public T get(int position) {
		Node temp = m_head;
		int counter = 0;
		while (temp != null && counter < position) {
			temp = temp.m_next;
			++counter;
		}
		return temp.m_data;
	}

	@Override
	public void set(int position, T data) {
		Node temp = m_head;
		int counter = 0;
		while (temp != null & counter < position) {
			temp = temp.m_next;
			++counter;
		}
		temp.m_data = data;
	}

	@Override
	public void push_front(T data) {
		if (m_head == null) {
			m_head = m_tail = new Node(data, null, null);
		} else {
			m_head = new Node(data, null, m_head);
		}
	}

	@Override
	public void push_back(T data) {
		if (m_tail == null) {
			m_tail = m_head = new Node(data, null, null);
		} else {
			m_tail = new Node(data, m_tail, null);
		}
	}

	@Override
	public void delete(int position) {
		Node temp = m_head;
		int counter = 0;
		while (temp != null && counter < position) {
			temp = temp.m_next;
			++counter;
		}
		if (temp == m_head){
			m_head = m_head.m_next;
			m_head.m_prev=null;
		}
		if (temp == m_tail){
			m_tail = m_tail.m_prev;
			m_tail.m_next=null;
		}
		else {
			temp.m_prev.m_next = temp.m_next;
			temp.m_next.m_prev = temp.m_prev;
		}
	}

	@Override
	public void print() {
		Node temp = m_head;
		while(temp!=null){
			System.out.println(temp.m_data);
			temp=temp.m_next;
		}
	}
}

class SoQ<T> extends MyList<T>{
	boolean isStack;
	
	SoQ(boolean isStack){
		this.isStack=isStack;
	}
	
	public void push(T data){
		if(isStack){
			push_front(data);
		}else{
			push_back(data);
		}
	}
	
	public T top(){
		if(isStack)
			return m_head.m_data;
		else
			return m_tail.m_data;
	}
}
